package com.byx.chatclient.adapter;

import android.text.TextUtils;
import android.widget.Adapter;
import android.widget.Filter;
import android.widget.Filterable;

import com.byx.chatclient.R;
import com.byx.chatclient.bean.SearchFriendBean;
import com.byx.chatclient.entity.Friend;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * SearchFriendAdapter
 *
 * @author Superb
 * @date 2021/4/27 14:06
 */
public class SearchFriendAdapter extends BaseQuickAdapter<SearchFriendBean, BaseViewHolder> implements Filterable {
    private static final int LAYOUT_RES_ID = R.layout.layout_search_friend_item;
    /**
     * 父类的列表仅用来显示数据，此好友列表用于搜索
     */
    private List<Friend> mFriends;
    private MyFilter mFilter;

    public SearchFriendAdapter(@Nullable List<SearchFriendBean> data) {
        super(LAYOUT_RES_ID, data);
    }

    public SearchFriendAdapter() {
        super(LAYOUT_RES_ID);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, SearchFriendBean searchFriendBean) {
        ((SimpleDraweeView) baseViewHolder.getView(R.id.sdv_avatar)).setImageURI(searchFriendBean.getAvatar());
        baseViewHolder.setText(R.id.tv_friend_remark,searchFriendBean.getRemark() == null ? searchFriendBean.getNickName() : searchFriendBean.getRemark());
        baseViewHolder.setText(R.id.tv_friend_nick_name,searchFriendBean.getNickName());
        baseViewHolder.setText(R.id.tv_friend_from,searchFriendBean.getFromFriendGroupName());
    }

    /**
     * 初始化好友列表
     * @param friends
     */
    public void setFriendsList(List<Friend> friends) {
        this.mFriends = friends;
    }

    /**
     * <p>Returns a filter that can be used to constrain data with a filtering
     * pattern.</p>
     *
     * <p>This method is usually implemented by {@link Adapter}
     * classes.</p>
     *
     * @return a filter used to constrain data
     */
    @Override
    public Filter getFilter() {
        if (mFilter == null){
            return new MyFilter();
        }
        return mFilter;
    }

    private class MyFilter extends Filter{

        /**
         * <p>Invoked in a worker thread to filter the data according to the
         * constraint. Subclasses must implement this method to perform the
         * filtering operation. Results computed by the filtering operation
         * must be returned as a {@link FilterResults} that
         * will then be published in the UI thread through
         * {@link #publishResults(CharSequence,
         * FilterResults)}.</p>
         *
         * <p><strong>Contract:</strong> When the constraint is null, the original
         * data must be restored.</p>
         *
         * @param constraint the constraint used to filter the data
         * @return the results of the filtering operation
         * @see #filter(CharSequence, FilterListener)
         * @see #publishResults(CharSequence, FilterResults)
         * @see FilterResults
         */
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults result = new FilterResults();
            List<SearchFriendBean> list = new ArrayList<>();
            if (!TextUtils.isEmpty(constraint) && mFriends != null) {//当过滤的关键字不为空的时候，我们则显示筛选的数据
                for (Friend friend : mFriends) {
                    if (friend.getFriendNickName().contains(constraint)) { //要匹配的item中的view
                        list.add(SearchFriendBean.fromFriend(friend));
                    }
                }
            }
            result.values = list; //将得到的集合保存到FilterResults的value变量中
            result.count = list.size();//将集合的大小保存到FilterResults的count变量中
            return result;
        }

        /**
         * <p>Invoked in the UI thread to publish the filtering results in the
         * user interface. Subclasses must implement this method to display the
         * results computed in {@link #performFiltering}.</p>
         *
         * @param constraint the constraint used to filter the data
         * @param results    the results of the filtering operation
         * @see #filter(CharSequence, FilterListener)
         * @see #performFiltering(CharSequence)
         * @see FilterResults
         */
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            setList((List<SearchFriendBean>) results.values);
        }
    }
}
